/*
 * TowerRedOne.java
 *
 * Created on December 9, 2007, 2:13 PM
 *
 * Copyright (C) 2007-2008  Hylke van der Schaaf
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, in version 3 of the License.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

package hextd.tower;

import hextd.hex.Hex;
import hextd.sprite.EnemySprite;
import hextd.util.Context;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;

/**
 *
 * @author hylke
 */
public class TowerRedOne extends TowerBase {
    
    public static int price = 200;
    public static int damage = 35;
    public static float range = 5f;
    public static float spreadRadiusBase = 2.75f;

    private int fireAt = -1;
    private float enemyX = 0;
    private float enemyY = 0;
    private EnemySprite enemy;
    private EnemySprite.COLOR damageColor = EnemySprite.COLOR.RED;
    private int coolDown = 0;
    private int coolDownMax = 4;
    
    private Color transColor;
    private Shape spread;
    private float spreadRadius;
    private float spreadRadius2;
    
    private EnemySprite[] currentTargets;
    
    /** Creates a new instance of TowerRedOne */
    public TowerRedOne(Context context, Hex hex) {
        super(TowerFactory.type.redOne, price, damage, range);
        this.lineColor = Color.RED;
        this.transColor = new Color(this.lineColor.getRed(), this.lineColor.getGreen(), this.lineColor.getBlue(), 75);
        this.doInit(context, hex);
    }
    
    public void doTick(int gameTime) {
        if (this.coolDown > 0) {
            this.coolDown--;
            if (this.coolDown > 3) {
                float radius = this.spreadRadius*(this.coolDown-3)/2;
                this.spread = new Ellipse2D.Float(this.enemy.getPaintX()-radius, this.enemy.getPaintY()-radius, radius*2, radius*2);
            }
        } else {
            int enemyNr = this.findEnemy(this.fireAt, false);
            this.fireAt = enemyNr;
            
            if (enemyNr >= 0) {
                float dx,dy,r;
                int splashDamage;
                
                this.enemy = this.context.enemies[enemyNr];
                this.enemyX = this.enemy.getX();
                this.enemyY = this.enemy.getY();
                this.currentTargets = this.findEnemiesInRange(this.enemyX, this.enemyY, this.spreadRadius);
                EnemySprite enemy2;
                for (int i=0; i<this.currentTargets.length; i++) {
                    enemy2 = this.currentTargets[i];
                    dx = this.enemyX - enemy2.getX();
                    dy = this.enemyY - enemy2.getY();
                    r = (float)Math.sqrt(dx*dx + dy*dy);
                    splashDamage = Math.round( this.damageCurrent * (1 - r/this.spreadRadius) );
                    enemy2.doDamage(splashDamage, this.damageColor);
                }
                
                //System.out.println("TR! fire at tick: "+gameTime);
                this.coolDown = this.coolDownMax;
                this.spread = new Ellipse2D.Float((this.enemy.getPaintX()-this.spreadRadius)*paintScale, (this.enemy.getPaintY()-this.spreadRadius)*paintScale, spreadRadius*2*paintScale, spreadRadius*2*paintScale);
            }
        }
    }
    
    public String getStatusString() {
        String retString = "";
        retString += "<h1>Red Tower 1</h1>" +
                "<p>" +
                "<b>Level:</b> "+(1+this.level)+"<br>\n" +
                "<b>Damage:</b> "+this.damageText+"<br>\n" +
                "<b>Range:</b> "+this.rangeText+"<br>" +
                "</p>\n";
        return retString;
    }
    
    public String getInfoString() {
        String retString = "";
        retString += "<h1>Red Tower 1</h1>" +
                "<p>" +
                "Tower with one laser. " +
                "The impact of this laser does splash damage to all enemies surrounding the target." +
                "<br>Strong against red enemies, very weak against green enemies." +
                "</p>" +
                "<p>" +
                "<b>Price:</b> "+TowerRedOne.price+"<br>" +
                "<b>Damage:</b> "+this.damageCurrent+" + splash<br>" +
                "<b>Range:</b> "+TowerRedOne.form.format(rangeCurrent)+"<br>" +
                "<b>Refire rate:</b> 4/s"+
                "</p>";
        return retString;
    }
    
    protected void generateShapes() {
        int shapeCount = 3;
        this.shapes = new Shape[shapeCount];
        this.shapes[0] = new Ellipse2D.Float(-0.5f*this.paintScale, -0.5f*this.paintScale, 1.0f*this.paintScale, 1.0f*this.paintScale);
        this.shapes[1] = new Ellipse2D.Float(-0.5f*this.paintScale, -0.5f*this.paintScale, 1.0f*this.paintScale, 1.0f*this.paintScale);
        this.shapes[2] = new Ellipse2D.Float(-1.0f*this.context.strokeScale, -1.0f*this.context.strokeScale, 2.0f*this.context.strokeScale, 2.0f*this.context.strokeScale);
        
        this.shapeAction = new int[shapeCount];
        this.shapeAction[0] = 2;
        this.shapeAction[1] = 1;
        this.shapeAction[2] = 1;
        
        this.shapeColor = new Color[shapeCount];
        this.shapeColor[0] = this.transColor;
        this.shapeColor[1] = this.lineColor;
        this.shapeColor[2] = this.lineColor;
        
        this.strokes = new Stroke[shapeCount];
        this.strokes[0] = new BasicStroke(2.0f*this.context.strokeScale, BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL);
        this.strokes[1] = this.strokes[0];
        this.strokes[2] = this.strokes[0];
        this.spreadRadius = TowerRedOne.spreadRadiusBase;
        this.spreadRadius2 = this.spreadRadius*this.spreadRadius;
    }
    
    public void paintEffects(Graphics2D g2, int gameTime) {
        Stroke defaultStroke = g2.getStroke();
        
        if (this.fireAt > -1 && this.coolDown == this.coolDownMax) {
            g2.setColor(this.lineColor);
            float ex = this.enemy.getPaintX()+( (float)(Math.random()-0.5)*this.paintScale/10f );
            float ey = this.enemy.getPaintY()+( (float)(Math.random()-0.5)*this.paintScale/10f );
            g2.draw(new Line2D.Float(this.xPaint, this.yPaint, ex, ey));
            for (int i=0; i<this.currentTargets.length; i++) {
                g2.draw(new Line2D.Float( ex, ey, currentTargets[i].getPaintX(), currentTargets[i].getPaintY() ));
            }
        }
        if (this.coolDown > this.coolDownMax-1) {
            g2.setColor(this.transColor);
            g2.draw(this.spread);
        }
        
        g2.setStroke(defaultStroke);
    }
}
